package comp512.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import comp512.classes.Player;
import comp512.classes.TimerLabel;

@SuppressWarnings("serial")
public class ClientGUI_Session extends JFrame implements Runnable{

	private JPanel contentPane;
	private JTextField txtTeamChatBox;
	private JTextField txtGlobalChatBox;
	private JTextField txtAnswers;
	private JTextField txtScore1;
	private JTextField txtScore2;
	private JTextField txtScore3;
	private JTextField txtScore4;
	private JTextField txtScore5;
	private JTextField txtScore6;
	private JTextField txtScore7;
	private JTextField txtScore8;
	private JTextField txtScore9;
	private JTextField txtScore10;
	private Player player;

	/**
	 * Create the frame.
	 */
	public ClientGUI_Session(Player player) {
     	setPlayer(player);
     	new Thread(this).start();
		setTitle("Family Feud Game Session");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 500, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JSplitPane baseSplitPane = new JSplitPane();
		baseSplitPane.setResizeWeight(0.5);
		baseSplitPane.setMinimumSize(new Dimension(5000, 5000));
		contentPane.add(baseSplitPane, BorderLayout.CENTER);

		JSplitPane splitChat = new JSplitPane();
		splitChat.setMaximumSize(new Dimension(200, 200));
		splitChat.setPreferredSize(new Dimension(200, 200));
		splitChat.setMinimumSize(new Dimension(200, 200));
		splitChat.setResizeWeight(0.5d);
		splitChat.setOrientation(JSplitPane.VERTICAL_SPLIT);
		baseSplitPane.setRightComponent(splitChat);

		JPanel pnlTeamChat = new JPanel();
		pnlTeamChat.setMinimumSize(new Dimension(5000, 5000));
		splitChat.setLeftComponent(pnlTeamChat);
		GridBagLayout gbl_pnlTeamChat = new GridBagLayout();
		gbl_pnlTeamChat.columnWidths = new int[] { 0, 0 };
		gbl_pnlTeamChat.rowHeights = new int[] { 0, 0, 0 };
		gbl_pnlTeamChat.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gbl_pnlTeamChat.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		pnlTeamChat.setLayout(gbl_pnlTeamChat);

		JLabel lblTeamChat = new JLabel("Team Chat");
		GridBagConstraints gbc_lblTeamChat = new GridBagConstraints();
		gbc_lblTeamChat.insets = new Insets(0, 0, 5, 0);
		gbc_lblTeamChat.gridx = 0;
		gbc_lblTeamChat.gridy = 0;
		pnlTeamChat.add(lblTeamChat, gbc_lblTeamChat);

		txtTeamChatBox = new JTextField();

		JScrollPane scrollTeamChat = new JScrollPane();
		GridBagConstraints gbc_scrollTeamChat = new GridBagConstraints();
		gbc_scrollTeamChat.insets = new Insets(0, 0, 5, 0);
		gbc_scrollTeamChat.fill = GridBagConstraints.BOTH;
		gbc_scrollTeamChat.gridx = 0;
		gbc_scrollTeamChat.gridy = 1;
		pnlTeamChat.add(scrollTeamChat, gbc_scrollTeamChat);

		final JTextPane txtTeamChatView = new JTextPane();
		scrollTeamChat.setViewportView(txtTeamChatView);
		txtTeamChatView.setEditable(false);
		txtTeamChatView.setBorder(new BevelBorder(BevelBorder.LOWERED,
				Color.DARK_GRAY, Color.LIGHT_GRAY, null, null));
		txtTeamChatBox.setBackground(Color.GREEN);
		txtTeamChatBox.setBorder(UIManager.getBorder("TextField.border"));
		GridBagConstraints gbc_txtTeamChatBox = new GridBagConstraints();
		gbc_txtTeamChatBox.anchor = GridBagConstraints.SOUTH;
		gbc_txtTeamChatBox.insets = new Insets(0, 0, 5, 0);
		gbc_txtTeamChatBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTeamChatBox.gridx = 0;
		gbc_txtTeamChatBox.gridy = 2;
		pnlTeamChat.add(txtTeamChatBox, gbc_txtTeamChatBox);
		txtTeamChatBox.setColumns(10);

		JPanel pnlGlobalChat = new JPanel();
		pnlGlobalChat.setMinimumSize(new Dimension(5000, 5000));
		splitChat.setRightComponent(pnlGlobalChat);
		GridBagLayout gbl_pnlGlobalChat = new GridBagLayout();
		gbl_pnlGlobalChat.columnWidths = new int[] { 0, 0 };
		gbl_pnlGlobalChat.rowHeights = new int[] { 0, 0, 0 };
		gbl_pnlGlobalChat.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gbl_pnlGlobalChat.rowWeights = new double[] { 0.0, 1.0,
				Double.MIN_VALUE };
		pnlGlobalChat.setLayout(gbl_pnlGlobalChat);

		JLabel lblGlobalChat = new JLabel("Global Chat");
		GridBagConstraints gbc_lblGlobalChat = new GridBagConstraints();
		gbc_lblGlobalChat.insets = new Insets(0, 0, 5, 0);
		gbc_lblGlobalChat.gridx = 0;
		gbc_lblGlobalChat.gridy = 0;
		pnlGlobalChat.add(lblGlobalChat, gbc_lblGlobalChat);

		JScrollPane scrollGlobalChat = new JScrollPane();
		GridBagConstraints gbc_scrollGlobalChat = new GridBagConstraints();
		// gbc_scrollGlobalChat.anchor = GridBagConstraints.SOUTH;
		gbc_scrollGlobalChat.insets = new Insets(0, 0, 5, 0);
		gbc_scrollGlobalChat.fill = GridBagConstraints.BOTH;
		gbc_scrollGlobalChat.gridx = 0;
		gbc_scrollGlobalChat.gridy = 1;
		pnlGlobalChat.add(scrollGlobalChat, gbc_scrollGlobalChat);

		final JTextPane txtGlobalChatView = new JTextPane();
		scrollGlobalChat.setViewportView(txtGlobalChatView);
		txtGlobalChatView.setEditable(false);
		txtGlobalChatView.setBorder(new BevelBorder(BevelBorder.LOWERED,
				Color.DARK_GRAY, Color.LIGHT_GRAY, null, null));

		txtGlobalChatBox = new JTextField();

		txtGlobalChatBox.setBackground(Color.ORANGE);
		GridBagConstraints gbc_txtGlobalChatBox = new GridBagConstraints();
		gbc_txtGlobalChatBox.insets = new Insets(0, 0, 5, 0);
		gbc_txtGlobalChatBox.anchor = GridBagConstraints.ABOVE_BASELINE;
		gbc_txtGlobalChatBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtGlobalChatBox.gridx = 0;
		gbc_txtGlobalChatBox.gridy = 2;
		pnlGlobalChat.add(txtGlobalChatBox, gbc_txtGlobalChatBox);
		txtGlobalChatBox.setColumns(10);

		txtGlobalChatBox.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
			}
		});

		JSplitPane splitContent = new JSplitPane();
		splitContent.setMinimumSize(new Dimension(225, 500));
		splitContent.setOrientation(JSplitPane.VERTICAL_SPLIT);
		baseSplitPane.setLeftComponent(splitContent);

		JPanel panelStatistics = new JPanel();
		panelStatistics
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		panelStatistics.setMinimumSize(new Dimension(500, 5000));
		splitContent.setLeftComponent(panelStatistics);
		panelStatistics.setLayout(new BorderLayout(5, 5));

		TimerLabel lblTimer = new TimerLabel();
		lblTimer.setHorizontalAlignment(SwingConstants.LEFT);
		lblTimer.setBorder(new EmptyBorder(0, 10, 0, 0));
		panelStatistics.add(lblTimer, BorderLayout.WEST);

		JLabel lblLivesXxx = new JLabel("Lives: XXX");
		lblLivesXxx.setBorder(new EmptyBorder(0, 0, 0, 10));
		panelStatistics.add(lblLivesXxx, BorderLayout.EAST);

		JLabel lblScore = new JLabel("Score: 0000");
		lblScore.setHorizontalTextPosition(SwingConstants.CENTER);
		lblScore.setHorizontalAlignment(SwingConstants.CENTER);
		panelStatistics.add(lblScore, BorderLayout.CENTER);

		JPanel panelQA = new JPanel();
		panelQA.setMinimumSize(new Dimension(5000, 5000));
		splitContent.setRightComponent(panelQA);
		panelQA.setLayout(new BorderLayout(0, 0));

		JTextPane txtpnQuestion = new JTextPane();
		txtpnQuestion.setEditable(false);
		txtpnQuestion.setText("Question");
		panelQA.add(txtpnQuestion, BorderLayout.NORTH);

		JPanel panel = new JPanel();
		panelQA.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new BorderLayout(0, 0));

		txtAnswers = new JTextField();
		panel.add(txtAnswers);
		txtAnswers.setText("Answers");
		txtAnswers.setColumns(10);

		JButton btnBuzz = new JButton("Buzz");
		panel.add(btnBuzz, BorderLayout.EAST);

		JSplitPane splitPane = new JSplitPane();
		splitPane.setMinimumSize(new Dimension(5000, 5000));
		splitPane.setResizeWeight(0.5);
		panelQA.add(splitPane, BorderLayout.CENTER);

		JPanel panelAnswersLeft = new JPanel();
		panelAnswersLeft.setMinimumSize(new Dimension(5000, 5000));
		splitPane.setLeftComponent(panelAnswersLeft);
		panelAnswersLeft.setLayout(new BoxLayout(panelAnswersLeft,
				BoxLayout.Y_AXIS));

		JPanel panelAnswer1 = new JPanel();
		JPanel panelAnswer2 = new JPanel();
		JPanel panelAnswer3 = new JPanel();
		JPanel panelAnswer4 = new JPanel();
		JPanel panelAnswer5 = new JPanel();

		panelAnswersLeft.add(panelAnswer1);
		panelAnswer1.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ1 = new JLabel("1.");
		lblIDQ1.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelAnswer1.add(lblIDQ1, BorderLayout.WEST);

		txtScore1 = new JTextField();
		txtScore1.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore1.setMinimumSize(new Dimension(5, 10));
		txtScore1.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore1.setEditable(false);
		txtScore1.setText("000");
		panelAnswer1.add(txtScore1, BorderLayout.EAST);
		txtScore1.setColumns(3);

		JLabel lblAnswer1 = new JLabel("Answer1");
		lblAnswer1.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer1.add(lblAnswer1, BorderLayout.CENTER);
		panelAnswersLeft.add(Box.createRigidArea(new Dimension(0, 5)));
		panelAnswersLeft.add(panelAnswer2);
		panelAnswer2.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ2 = new JLabel("2.");
		lblIDQ2.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelAnswer2.add(lblIDQ2, BorderLayout.WEST);

		txtScore2 = new JTextField();
		txtScore2.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore2.setMinimumSize(new Dimension(5, 10));
		txtScore2.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore2.setText("000");
		txtScore2.setEditable(false);
		txtScore2.setColumns(3);
		panelAnswer2.add(txtScore2, BorderLayout.EAST);

		JLabel lblAnswer2 = new JLabel("Answer2");
		lblAnswer2.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer2.add(lblAnswer2, BorderLayout.CENTER);
		panelAnswersLeft.add(Box.createRigidArea(new Dimension(0, 5)));
		panelAnswersLeft.add(panelAnswer3);
		panelAnswer3.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ3 = new JLabel("3.");
		lblIDQ3.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelAnswer3.add(lblIDQ3, BorderLayout.WEST);

		txtScore3 = new JTextField();
		txtScore3.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore3.setMinimumSize(new Dimension(5, 10));
		txtScore3.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore3.setText("000");
		txtScore3.setEditable(false);
		txtScore3.setColumns(3);
		panelAnswer3.add(txtScore3, BorderLayout.EAST);

		JLabel lblAnswer3 = new JLabel("Answer3");
		lblAnswer3.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer3.add(lblAnswer3, BorderLayout.CENTER);
		panelAnswersLeft.add(Box.createRigidArea(new Dimension(0, 5)));
		panelAnswersLeft.add(panelAnswer4);
		panelAnswer4.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ4 = new JLabel("4.");
		lblIDQ4.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelAnswer4.add(lblIDQ4, BorderLayout.WEST);

		txtScore4 = new JTextField();
		txtScore4.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore4.setMinimumSize(new Dimension(5, 10));
		txtScore4.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore4.setText("000");
		txtScore4.setEditable(false);
		txtScore4.setColumns(3);
		panelAnswer4.add(txtScore4, BorderLayout.EAST);

		JLabel lblAnswer4 = new JLabel("Answer4");
		lblAnswer4.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer4.add(lblAnswer4, BorderLayout.CENTER);
		panelAnswersLeft.add(Box.createRigidArea(new Dimension(0, 5)));
		panelAnswersLeft.add(panelAnswer5);
		panelAnswer5.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ5 = new JLabel("5.");
		lblIDQ5.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelAnswer5.add(lblIDQ5, BorderLayout.WEST);

		txtScore5 = new JTextField();
		txtScore5.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore5.setMinimumSize(new Dimension(5, 10));
		txtScore5.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore5.setText("000");
		txtScore5.setEditable(false);
		txtScore5.setColumns(3);
		panelAnswer5.add(txtScore5, BorderLayout.EAST);

		JLabel lblAnswer5 = new JLabel("Answer5");
		lblAnswer5.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer5.add(lblAnswer5, BorderLayout.CENTER);

		JPanel panelAnswersRight = new JPanel();
		panelAnswersRight.setMinimumSize(new Dimension(5000, 5000));
		splitPane.setRightComponent(panelAnswersRight);
		panelAnswersRight.setLayout(new BoxLayout(panelAnswersRight,
				BoxLayout.Y_AXIS));

		JPanel panelAnswer6 = new JPanel();
		JPanel panelAnswer7 = new JPanel();
		JPanel panelAnswer8 = new JPanel();
		JPanel panelAnswer9 = new JPanel();
		JPanel panelAnswer10 = new JPanel();

		panelAnswersRight.add(panelAnswer6);
		panelAnswer6.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ6 = new JLabel("6.");
		lblIDQ6.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelAnswer6.add(lblIDQ6, BorderLayout.WEST);

		txtScore6 = new JTextField();
		txtScore6.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore6.setMinimumSize(new Dimension(5, 10));
		txtScore6.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore6.setText("000");
		txtScore6.setEditable(false);
		txtScore6.setColumns(3);
		panelAnswer6.add(txtScore6, BorderLayout.EAST);

		JLabel lblAnswer6 = new JLabel("Answer6");
		lblAnswer6.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer6.add(lblAnswer6, BorderLayout.CENTER);
		panelAnswersRight.add(Box.createRigidArea(new Dimension(0, 5)));
		panelAnswersRight.add(panelAnswer7);
		panelAnswer7.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ7 = new JLabel("7.");
		lblIDQ7.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelAnswer7.add(lblIDQ7, BorderLayout.WEST);

		txtScore7 = new JTextField();
		txtScore7.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore7.setMinimumSize(new Dimension(5, 10));
		txtScore7.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore7.setText("000");
		txtScore7.setEditable(false);
		txtScore7.setColumns(3);
		panelAnswer7.add(txtScore7, BorderLayout.EAST);
		JLabel lblAnswer7 = new JLabel("Answer7");
		lblAnswer7.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer7.add(lblAnswer7, BorderLayout.CENTER);
		panelAnswersRight.add(Box.createRigidArea(new Dimension(0, 5)));
		panelAnswersRight.add(panelAnswer8);
		panelAnswer8.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ8 = new JLabel("8.");
		lblIDQ8.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelAnswer8.add(lblIDQ8, BorderLayout.WEST);

		txtScore8 = new JTextField();
		txtScore8.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore8.setMinimumSize(new Dimension(5, 10));
		txtScore8.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore8.setText("000");
		txtScore8.setEditable(false);
		txtScore8.setColumns(3);
		panelAnswer8.add(txtScore8, BorderLayout.EAST);
		JLabel lblAnswer8 = new JLabel("Answer8");
		lblAnswer8.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer8.add(lblAnswer8, BorderLayout.CENTER);
		panelAnswersRight.add(Box.createRigidArea(new Dimension(0, 5)));
		panelAnswersRight.add(panelAnswer9);
		panelAnswer9.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ9 = new JLabel("9.");
		lblIDQ9.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelAnswer9.add(lblIDQ9, BorderLayout.WEST);

		txtScore9 = new JTextField();
		txtScore9.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore9.setMinimumSize(new Dimension(5, 10));
		txtScore9.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore9.setText("000");
		txtScore9.setEditable(false);
		txtScore9.setColumns(3);
		panelAnswer9.add(txtScore9, BorderLayout.EAST);
		JLabel lblAnswer9 = new JLabel("Answer9");
		lblAnswer9.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer9.add(lblAnswer9, BorderLayout.CENTER);
		panelAnswersRight.add(Box.createRigidArea(new Dimension(0, 5)));
		panelAnswersRight.add(panelAnswer10);
		panelAnswer10.setLayout(new BorderLayout(0, 0));

		JLabel lblIDQ10 = new JLabel("10.");
		lblIDQ10.setBorder(new EmptyBorder(0, 5, 0, 0));
		panelAnswer10.add(lblIDQ10, BorderLayout.WEST);

		txtScore10 = new JTextField();
		txtScore10.setBorder(new LineBorder(Color.BLACK, 2));
		txtScore10.setMinimumSize(new Dimension(5, 10));
		txtScore10.setHorizontalAlignment(SwingConstants.RIGHT);
		txtScore10.setText("000");
		txtScore10.setEditable(false);
		txtScore10.setColumns(3);
		panelAnswer10.add(txtScore10, BorderLayout.EAST);
		JLabel lblAnswer10 = new JLabel("Answer10");
		lblAnswer10.setHorizontalAlignment(SwingConstants.CENTER);
		panelAnswer10.add(lblAnswer10, BorderLayout.CENTER);
		txtTeamChatBox.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER && txtTeamChatBox.getText().length() > 0) {
					String currentChat = txtTeamChatView.getText();
					currentChat += "\n" + txtTeamChatBox.getText();
					txtTeamChatView.setText(currentChat);
					txtTeamChatBox.setText("");
				}
			}
		});
		
		txtGlobalChatBox.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent arg0){
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER && txtGlobalChatBox.getText().length() > 0) {
					String currentChat = txtGlobalChatView.getText();
					currentChat += "\n" + txtGlobalChatBox.getText();
					txtGlobalChatView.setText(currentChat);
					txtGlobalChatBox.setText("");
				}
			}
		});
		
		lblTimer.startTimer(30);
	}

	private void setPlayer(Player player) {
		this.player = player;
		
	}

	@Override
	public void run() {
		StringBuilder globalMessagesBuilder = new StringBuilder();
		for(String globalMessage: player.getGlobalMessasages()){
			globalMessagesBuilder.append(globalMessage);
			globalMessagesBuilder.append("\n");
		}
		txtGlobalChatBox.setText(globalMessagesBuilder.toString());
		
		StringBuilder teamMessagesBuilder = new StringBuilder();
		for(String teamMessage: player.getTeamMessages()){
			teamMessagesBuilder.append(teamMessage);
			teamMessagesBuilder.append("\n");
		}
		txtTeamChatBox.setText(teamMessagesBuilder.toString());
		
	}
	
	
}
